<?php
/**
 * 定时任务
 * @author Administrator
 *
 */
class Crontab extends Controller{
    var $user = null;
    function Crontab(){
        parent::Controller();
        $this->user = array(
            'userId'    => 0,//用户ID
            'userName'  => '',//用户名称
            'userRole'  => 0,//用户角色
            'userLimit' => array(),//用户权限
        );
    }
    /**
     * 预算自动审核
     */
    function budgetAutoAudit(){
        $this->budgetSingleProjectAutoAudit();//项目预算自动审核
    }
    /**
     * 项目预算自动审核
     */
    function budgetSingleProjectAutoAudit(){
        $this->load->model('sProjectModel');
        $this->load->model('sPGxModel');
        $this->load->model('sPOfflineModel');
        //项目ID集
        $projectIds = array();
        $time = strtotime('-1 day');
        echo "<font color='red'>开始项目预算自动审核</font><br>";
        //广宣ID集
        $gxList = $this->sPGxModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time));
        foreach ($gxList as $v){
            $projectIds[(int)$v['projectId']]['gx'][] = (int)$v['id'];
        }
        //线下ID集
        $offlineList = $this->sPOfflineModel->getList(array('budgetAuditRs'=>0,'budgetSubTime < '=>$time));
        foreach ($offlineList as $v){
            $projectIds[(int)$v['projectId']]['offline'][] = (int)$v['id'];
        }
        if ($projectIds){
            $projectList = $this->sProjectModel->getList(array('id'=>array('in', array_keys($projectIds) )));
            foreach ($projectList as $v){
                if (isset($projectIds[$v['id']]['gx'])){
                    foreach ($projectIds[$v['id']]['gx'] as $gxv){
                        $this->sPGxModel->budgetAudit($gxv, 1, $this->user);
                    }
                }
                if (isset($projectIds[$v['id']]['offline'])){
                    foreach ($projectIds[$v['id']]['offline'] as $offlinev){
                        $this->sPOfflineModel->budgetAudit($offlinev, 1, $this->user);
                    }
                }
                $this->sProjectModel->budgetUpdateStatus($v['id']);
                $this->sProjectModel->updateMoney($v['id']);
                echo "项目方案编号为【".$v['id']."】预算自动审核成功<br> \n";
                usleep(500);
            }
            //暂停500微妙
            echo str_repeat(" ",4096);
        }
        echo "<font color='red'>结束项目预算自动审核</font><br>";
    }
}